class Argument < ActiveRecord::Base
  
  @@last_random_response = nil
  
  has_many :responses
  belongs_to :user
  validates_presence_of :body
  
  belongs_to :language, :foreign_key => 'language_id',
                        :class_name => '::Globalize::Language'
  
  before_save :set_language
  
  def set_language
    if Locale.active && Locale.active.language
      self.language = Locale.active.language
    else
      self.language = Locale.base_language
    end
  end

  def random_response
    count = responses.count(:all) 
    if count < 1
      return nil
    elsif count == 1
      return responses.first
    elsif count > 1
      loop do 
        offset = rand(count) 
        @response = responses.find :first, :offset => offset
        if @response != @@last_random_response
          @@last_random_response = @response
          return @response
        end
      end
    end
  end
  
end
